import numpy as np
from .opts import ColorModeType,DeviceType,StatusCode
import cv2
#import czcv
import time
#
# def single_cut_slim(
#                jpeg_array : np.array,
#                color_type  : ColorModeType,
#                device_type : DeviceType):
#     '''
#     czcv 单页裁剪精简版
#     :param jpeg_array:   jpeg 数据
#     :param color_type:    颜色模式类型
#     :param device_type:   设备类型
#     :return:  status: StatusCode,  base_cutted_im : np.array,  colored_im : np.array
#     '''
#     #im = cv2.imdecode(jpeg_array, cv2.IMREAD_COLOR)
#     #print(im.shape)
#     jpeg_array = jpeg_array[np.newaxis,:]
#     start = time.clock()
#     flg, base, colored = czcv.single_cut_slim(jpeg_array, int(color_type), int(device_type))
#     elapsed = (time.clock() - start)
#     print("single_cut_slim time used:", elapsed)
#
#     return  flg, base, colored
#
#     #print(base.shape)


def single_cut_slim(
               img,
               color_type,
               device_type):
    '''
    czcv 单页裁剪精简版
    :param img:
    :param color_type:    颜色模式类型
    :param device_type:   设备类型
    :return:  status: StatusCode,  base_cutted_im : np.array,  colored_im : np.array
    '''
    base  = img
    colored = img
    flg = 0

    if color_type == ColorModeType.COLOR:
        colored = img
    elif color_type == ColorModeType.GRAY:
        colored = cv2.cvtColor(base,cv2.COLOR_BGR2GRAY)
        colored = cv2.cvtColor(colored,cv2.COLOR_GRAY2BGR )

    elif color_type ==ColorModeType.BW:
        colored = cv2.cvtColor(base,  cv2.COLOR_BGR2GRAY)
        cv2.threshold(colored,  0, 255, cv2.THRESH_OTSU, colored)
        colored =  cv2.cvtColor(colored, cv2.COLOR_GRAY2BGR)

    return  flg, base, colored


